package com.ruoyi.algorithm.service.impl;

import com.ruoyi.algorithm.annotation.TableStructAnno;
import com.ruoyi.algorithm.entity.TableStruct;
import com.ruoyi.algorithm.entity.UserTableStruct;
import com.ruoyi.algorithm.repository.TableStructRepository;
import com.ruoyi.algorithm.repository.UserTableStructRepository;
import com.ruoyi.algorithm.service.TableStructService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：yanfa-03
 * @date ：Created in 2021/7/12/9:41
 * @description ：表结构服务
 * @version: 1.0
 */
@Service
public class TableStructServiceImpl implements TableStructService {

    @Autowired
    private TableStructRepository tableStructRepository;

    @Autowired
    private UserTableStructRepository userTableStructRepository;


    /**
     * 获取表结构与用户关联信息
     *
     * @param userId 用户ID
     * @param tableName 目标带有的@TableStructAnno注解value
     * @return 实体类结构列表
     */
    @Override
    public List<TableStruct> getTableField(Long userId, String tableName) {
        List<TableStruct> tableStructs = tableStructRepository.getByTableName(tableName);
        for (TableStruct tableStruct : tableStructs) {
            Long structId = tableStruct.getId();
            UserTableStruct userTableStruct = userTableStructRepository.getByUserIdAndStructId(userId, structId);
            if (ObjectUtils.isEmpty(userTableStruct)) {
                return null;
            }
            tableStruct.setId(userTableStruct.getId());
            tableStruct.setDisplay(userTableStruct.getDisplay());
            tableStruct.setSeq(userTableStruct.getSeq());
        }
        return tableStructs;
    }

    /**
     * 新增表结构
     *
     * @param target 目标实体类
     * @return 成功与否
     */
    @Override
    public void addTableStructs(Class<?> target) {
        try {
            //insert into table_struct
            List<TableStruct> tableStructs = new ArrayList<>();
            //getTableName
            TableStructAnno tableStructAnno = target.getAnnotation(TableStructAnno.class);
            if (ObjectUtils.isEmpty(tableStructAnno)) {
                return;
            }
            String tableName = tableStructAnno.value();
            //getFields
            Field[] fields = target.getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                Field field = fields[i];
                TableStructAnno apiModelProperty = field.getAnnotation(TableStructAnno.class);
                if (ObjectUtils.isEmpty(apiModelProperty)) {
                    continue;
                }
                String fieldName = apiModelProperty.value();
                String fieldKey = field.getName();
                TableStruct tableStruct = new TableStruct(tableName, fieldKey, fieldName, false);
                List<TableStruct> byTableName = tableStructRepository.getByTableName(tableName, tableStruct.getFieldKey());
                if (byTableName.size() > 0) {
                    continue;
                }
                tableStructs.add(tableStruct);
            }
            //insert
            tableStructRepository.saveBatch(tableStructs);

            //insert into user_table_struct
        } catch (SecurityException e) {
            e.printStackTrace();
        }
    }


}
